package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_2_1;
import com.hongxin.nw.netty.mq_info.domain.CMD202;
import com.hongxin.nw.netty.utils.DateUtil;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.ModifyJsonKeyUtil;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 结束充电202
 * @author: GuoXiaolin
 * @date 2019/6/21 19:32
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class EndCharge implements IMq {
    @Override
    public JSONObject sendMqInfo(Transfer transfer) throws Exception {
        //业务系统对应的启动方式
        String card = "0";//刷卡
        String remote = "1";//远程
        String vin = "4";//vin
        //json字段
        String jsonVin = "VIN";//业务系统需要的字段
        String jsonRemoveVin= "vIN";//业务系统不需要的字段

        Mrena1_2_1 dataItem = (Mrena1_2_1) transfer.getObj();
        CMD202 cmd202 = new CMD202();
        cmd202.setMachineId(dataItem.getCo());
        cmd202.setCpOrderId(dataItem.getTran_num());
        cmd202.setInterfaceNumber(String.valueOf(Integer.parseInt(dataItem.getCharge_interface_identify())+1));cmd202.setVIN(dataItem.getVin());
        int st = Integer.parseInt(dataItem.getStartType());//充电启动方式
        if (st == TypeIdentif.START_TYPE_CARD.getInt()){//刷卡:返回卡号
            cmd202.setUserId(dataItem.getPhysics_num());
            cmd202.setStartMode(card);//启动方式
        }
        if (st == TypeIdentif.START_TYPE_REMOTE.getInt()){//远程:返回userid
            cmd202.setUserId(dataItem.getUserId());
            cmd202.setStartMode(remote);
        }
        if (st == TypeIdentif.START_TYPE_VIN.getInt()){//vin:返回vin
            cmd202.setUserId(dataItem.getVin());
            cmd202.setStartMode(vin);
        }
        cmd202.setStartDateTime(dataItem.getStart_time());
        cmd202.setEndDateTime(dataItem.getEnd_time());
        cmd202.setStartSOC(dataItem.getStartSOC());
        cmd202.setEndSOC(dataItem.getEndSOC());
        cmd202.setChargingElectricQuantity(dataItem.getZ_eq());
        cmd202.setEleFee(dataItem.getCharge_z_ef());
        cmd202.setServiceFee(dataItem.getServer_money());
        cmd202.setParkingFee(dataItem.getOccupancy_money());
        cmd202.setFinishMode(dataItem.getCharge_end_reason());
        cmd202.setBeforeBalance(dataItem.getBalance_before_deduction());
        cmd202.setAfterBalance(dataItem.getBalance_after_deduction());
        cmd202.setPaid( dataItem.getWallet_tran_num());
        cmd202.setNumberPlate(dataItem.getVin());
        cmd202.setTotalFee(dataItem.getCharge_z_fee());
        String[] s1 = new String[]{dataItem.getJ_eq(), dataItem.getF_eq(), dataItem.getP_eq(), dataItem.getG_eq()};
        cmd202.setTimingElectricQuantity(JSONObject.toJSONString(s1));
        cmd202.setInterfaceType(TypeIdentif.E_TYPE_DIRECT.getStr());//2:直流,1:交流
        String cd = DateUtil.getSecond(dataItem.getStart_time().split("\\.")[0],dataItem.getEnd_time().split("\\.")[0]);
        cmd202.setChargingDuration(cd);

        JSONObject json = (JSONObject) JSONObject.toJSON(cmd202);
        json.put(jsonVin, cmd202.getVIN());
        json.put(Catalogue.HXCMD,Catalogue.CMD202);
        JSONObject mqJson = ModifyJsonKeyUtil.removeEmptyKey(json);
        mqJson.remove(jsonRemoveVin);

        return JSONUtils.getRoot(0, StaticValue.VAL_29, mqJson);
    }
}
